//
//  MeatMeHeader.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/3.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "MeatMeHeader.h"

@interface MeatMeHeader()

/**
 服务商Icon
 */
@property (nonatomic, strong) UIImageView *serviceIcon;


/**
 服务商lab
 */
@property (nonatomic, strong) UILabel *servicelab;


/**
 设置Icon
 */
@property (nonatomic, strong) UIImageView *setIcon;


/**
 消息Icon
 */
@property (nonatomic, strong) UIImageView *messageIcon;


/**
 头像Icon
 */
@property (nonatomic, strong) UIImageView *headIcon;


/**
 昵称
 */
@property (nonatomic, strong) UILabel *puLab;


/**
 性别
 */
@property (nonatomic, strong) UILabel *genderLab;


/**
 签名
 */
@property (nonatomic, strong) UILabel *signLab;


/**
 登录/注册
 */
@property (nonatomic, strong) UILabel *registLoginLab;



@property (nonatomic, strong) UIView *bottomView;


@end

@implementation MeatMeHeader

#pragma mark - lifecycle methods

- (void)setupUI
{
    UIImage *image = [UIImage imageNamed:@"user_headerviewbg"];
    self.layer.contents = (__bridge id)image.CGImage;
    
    [self addSubview:self.serviceIcon];
    [_serviceIcon addSubview:self.servicelab];
    [self addSubview:self.setIcon];
    [self addSubview:self.messageIcon];
    [self addSubview:self.registLoginLab];

    [self addSubview:self.bottomView];
    [self addSubview:self.headIcon];
    
    [self addSubview:self.puLab];
    [self addSubview:self.genderLab];
    [self addSubview:self.signLab];

}

- (void)setupLayout
{
    self.height = 164 + TJ_StatusBarHeight_Define;
    
    {
        _serviceIcon.size = CGSizeMake(95, 21);
        _serviceIcon.mj_y = TJ_StatusBarHeight_Define + 12;
        _serviceIcon.mj_x = 15;
    }
    
    
    {
        _servicelab.size = CGSizeMake(87, 21);
        _servicelab.mj_x = _servicelab.mj_y = 0;
        
    }
    
    {
        _messageIcon.size = CGSizeMake(40, 40);
        _messageIcon.mj_y = TJ_StatusBarHeight_Define;
        _messageIcon.mj_x = KScreenWidth - _messageIcon.width - 10;
    }
    
    {
        _setIcon.size = CGSizeMake(40, 40);
        _setIcon.mj_x = _messageIcon.left - _setIcon.width;
        _setIcon.mj_y = _messageIcon.mj_y;
    }
    
    
    {
        _bottomView.size = CGSizeMake(KScreenWidth - 30, 58);
        _bottomView.mj_x = 15;
        _bottomView.mj_y = self.height -_bottomView.height;
    }
    
    {
        _headIcon.size = CGSizeMake(76, 76);
        _headIcon.mj_x = _bottomView.mj_x + 15;
        _headIcon.mj_y = _bottomView.bottom - (_headIcon.height + 15);
    }
    
    {
        _registLoginLab.size = CGSizeMake(100, 18);
        _registLoginLab.mj_x = 20 + _headIcon.right;
        _registLoginLab.mj_y = self.height - 43 - _registLoginLab.height;
    }

    
    {
        _puLab.size = CGSizeMake(KScreenWidth - _headIcon.width - 20, 20);
        _puLab.mj_x = _headIcon.right + 6;
        _puLab.mj_y = _bottomView.top -( 10 + _puLab.height);
    }
    
    {
        _genderLab.size = CGSizeMake(_puLab.width, 18);
        _genderLab.mj_x = _puLab.mj_x;
        _genderLab.mj_y = _bottomView.top + 6;
    }
    
    {
        _signLab.size = CGSizeMake(_puLab.width, 18);
        _signLab.mj_x = _puLab.mj_x;
        _signLab.mj_y = _genderLab.bottom + 10;
    }

    
//    {
//        NSMutableAttributedString *puStr = [@"带着壳的兔兔 " createAttributingByIcon:nil downIndexImage:@"meat_me_pu" textColor:KWhiteColor font:mFont(20)];
//        _puLab.attributedText = puStr;
//        
//        NSMutableAttributedString *genderStr = [@" 天津市武清区… 25岁 " createAttributingByIcon:@"user_location" downIndexImage:@"user_woman" textColor:UIColorFromRGB(0x707070) font:mFont(12)];
//        _genderLab.attributedText = genderStr;
//        
//        _signLab.text = @"编辑你的个性签名";
//    }
    
}


#pragma mark - setter methods

- (void)setType:(MeatMeHeaderType)type{
    
    _type = type;
    switch (type) {
        case MeatMeHeaderType_None:
            {
                _bottomView.hidden = YES;
                _signLab.hidden = YES;
                _genderLab.hidden = YES;
                _puLab.hidden = YES;
                _registLoginLab.hidden = NO;
                
                _headIcon.image = [UIImage imageNamed:@"user_header_default"];
                self.serviceIcon.width = 126;
                self.servicelab.width = 116;
                self.servicelab.text = @"申请成为服务商";
            }
            break;
            
        default:
            {
                _bottomView.hidden = NO;
                _signLab.hidden = NO;
                _genderLab.hidden = NO;
                _puLab.hidden = NO;
                _registLoginLab.hidden = YES;
                
               // _headIcon.image = [UIImage imageNamed:@"meat_me_login"];
                _serviceIcon.width = 95;
                _servicelab.width = 87;
                _servicelab.text = @"服务商管理";

            }
            break;
    }
}


- (void)setItem:(UserDetailInfo *)item{
    
    [_headIcon sd_setImageWithURL:[NSURL URLWithString:item.headImg] placeholderImage:[UIImage imageNamed:@"placeholder_middle"]];

    {
        NSString *nickname = [NSString stringWithFormat:@"%@ ", item.nickName];
        NSMutableAttributedString *puStr = [nickname createAttributingByIcon:nil downIndexImage:@"meat_me_pu" textColor:KWhiteColor font:mFont(20)];
        _puLab.attributedText = puStr;
    }

    {
        NSString *location = [NSString stringWithFormat:@" %@  %@  ", item.regionName, item.age];
        NSString *sexIcon = [item.sex isEqual:@2] ?@"user_woman" :@"user_man";
        NSMutableAttributedString *genderStr = [location createAttributingByIcon:@"user_location" downIndexImage:sexIcon textColor:UIColorFromRGB(0x707070) font:mFont(12)];
        _genderLab.attributedText = genderStr;

    }
    
    {
        _signLab.text = item.personality;

    }
}


#pragma mark - getter methods

- (UIImageView *)serviceIcon{
    
    if (!_serviceIcon) {
        _serviceIcon = [UIImageView new];
        _serviceIcon.image = [UIImage imageNamed:@"meat_me_manager"];
        
        @weakify(self);
        _serviceIcon.tapBlock = ^{
            @strongify(self);
            if (self.dealServiceBlock) {
                self.dealServiceBlock();
            }
        };
    }
        
    return _serviceIcon;
}

- (UILabel *)servicelab{
    
    if (!_servicelab) {
        _servicelab = [UILabel makeLabelWithText:@"服务商管理" textColor:KWhiteColor font:mFont(11) textAliment:2];
    }
    return _servicelab;
}

- (UIImageView *)setIcon{
    
    if (!_setIcon) {
        _setIcon = [UIImageView makeImageByImageName:@"user_setup" cornerRadius:0];
        
        @weakify(self);
        _setIcon.isNeedAnimation = YES;
        _setIcon.tapBlock = ^{
            @strongify(self);
            if (self.dealSetUpBlock) {
                self.dealSetUpBlock();
            }
        };
    }
    return _setIcon;
}

- (UIImageView *)messageIcon{
    
    if (!_messageIcon) {
        _messageIcon = [UIImageView makeImageByImageName:@"user_message" cornerRadius:0];
        
        @weakify(self);
        _messageIcon.isNeedAnimation = YES;
        _messageIcon.tapBlock = ^{
            @strongify(self);
            if (self.dealMessageBlock) {
                self.dealMessageBlock();
            }
        };
    }
    return _messageIcon;
}


- (UIImageView *)headIcon{
    
    if (!_headIcon) {
        _headIcon = [UIImageView new];
        _headIcon.image = [UIImage imageNamed:@"user_header_default"];
        [self addShadowAndCircleCorner:_headIcon.layer corner:38];
        
        @weakify(self);
        _headIcon.tapBlock = ^{
            @strongify(self);
            if (self.dealUserInformationBlock) {
                self.dealUserInformationBlock();
            }
        };

    }
    return _headIcon;
}


- (UIView *)bottomView{
    
    if (!_bottomView) {
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = KWhiteColor;
        
        CAShapeLayer *maskLayer = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, KScreenWidth-30, 58) byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:CGSizeMake(12, 12)];
        maskLayer.path = path.CGPath;
        _bottomView.layer.mask = maskLayer;
    }
    return _bottomView;
}


- (UILabel *)puLab{
    
    if (!_puLab) {
        _puLab = [[UILabel alloc] init];
    }
    return _puLab;
}


- (UILabel *)genderLab{
    
    if (!_genderLab) {
        _genderLab = [[UILabel alloc] init];
    }
    return _genderLab;
}


- (UILabel *)signLab{
    
    if (!_signLab) {
        _signLab = [UILabel makeLabelWithText:@"" textColor:UIColorFromRGB(0x707070) font:mFont(12) textAliment:0];
    }
    return _signLab;
}


- (UILabel *)registLoginLab{
    
    if (!_registLoginLab) {
        _registLoginLab = [UILabel makeLabelWithText:@"登录/注册" textColor:KWhiteColor font:mFont(18) textAliment:0];
        
        @weakify(self);
        _registLoginLab.isNeedAnimation = YES;
        _registLoginLab.tapBlock = ^{
            @strongify(self);
            if (self.dealRegistLoginBlock) {
                self.dealRegistLoginBlock();
            }
        };

    }
    return _registLoginLab;
}


@end
